var util = require('../../../utils/util');
const {
  postMolyApp
} = require('../../../api/user')
const avatar = JSON.parse(wx.getStorageSync('userInfo')).img
const molyAvatar = '../../../img/moly.png'
Page({
  data: {
    msg: '',
    scroll: [
      // {date:'123',text:'张1'},  
    ],
    toView: '',
  },
  handleInput(e){
    const msg = e.detail.value
    this.setData({msg})
  },
  handleSubmit(e){
    const msg = this.data.msg
    if(!msg){
      wx.showToast({
        title: '请输入内容',
        icon:'error'
      })
      return
    }
    this.getRobotMsg(msg)
    const time = util.formatTime(new Date());
    const temp = {
      date: time,
      text: msg,
      avatar,
      left: 0,
    }
    this.data.scroll.push(temp)
    this.setData({
      scroll: this.data.scroll,
      toView: "msg-" + (this.data.scroll.length - 1),
      msg: ''
    })
  },
  onLoad(options) {
    this.getRobotMsg('万金油')
  },
  //发送请求到茉莉云api
  async getRobotMsg(content) {
    const {
      data
    } = await postMolyApp({
      content,
      type: "1",
      from: "微信小程序"
    })
    const _content = data[0].content
    let time = util.formatTime(new Date());
    let temp = {
      date: time,
      text:_content,
      avatar:molyAvatar,
      left: 1,
    }
    this.data.scroll.push(temp)
    this.setData({
      scroll: this.data.scroll,
      toView: "msg-" + (this.data.scroll.length - 1),
    })
  },
  onReady() {

  },

  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})